package cn.shopping.controller.login;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shopping.pojo.Admin_type;
import cn.shopping.pojo.Admin_user;
import cn.shopping.service.login.adminService;

@Controller
@RequestMapping("/login")
public class AdminLogin {

	
	
	@Resource
	private adminService adminservice;
	
	
	/*
	 * 登录
	 */
	@RequestMapping("/toindex.html")
	public String login(@RequestParam(value="userCode") String userCode,@RequestParam(value="userpassword") String userpassword,Model model,HttpSession session){
		
		Admin_user adminusers = adminservice.getuser(userCode);
		if (adminusers==null) {
			model.addAttribute("无此账号");
			return "logins";
		}
		System.out.println(adminusers.getRoleName());
		if(!adminusers.getRolePassword().equals(userpassword)){
			model.addAttribute("密码错误");
			return "logins";
		}
		session.setAttribute("user",adminusers);
		return "index";
	}
	
	//管理�?
	/**
	 * 管理员类�?
	 */
	@RequestMapping(value="types",method=RequestMethod.GET)
	@ResponseBody
	public List<Admin_type> types() {
		List<Admin_type> list = adminservice.getalltype();
		return list;
	}
	/**
	 * 管理员显�?
	 */
	
	@RequestMapping(value="allofadmin.html")
	public String adminlist(HttpSession session, Model model){
		List<Admin_user> list = adminservice.getalluser(((Admin_user)session.getAttribute("user")).getRole());
		model.addAttribute("adminuser", list);
		return "guanliyuanliebiao";
	}
	
	
	/**
	 * 添加管理�?
	 */
	@RequestMapping(value="/addadmin.html",method=RequestMethod.POST)
	public String adminadd(@RequestParam(value="admin.qx")Integer ty_id,
			@RequestParam(value="admin.user")String au_name,
			@RequestParam(value="admin.password")String Password,
		    HttpSession session, Model model){
		Admin_user admin = new Admin_user();
		int role = (int)ty_id;
		admin.setRole(role);
		admin.setRoleName(au_name);
		admin.setRolePassword(Password);
		admin.setStartday(new Date());
		System.out.println(ty_id+""+au_name+""+Password+""+admin.getStartday());
		boolean resoult = adminservice.addadmin(admin);
		if(!resoult){
			return "tianjiaguanliyuan";
		}
		return "redirect:/login/allofadmin.html";
	}
	
	/**
	 * 修改管理�?
	 */
	
	//获取管理员信�?
	@RequestMapping("/getadminbyid.html/{uid}")
	public String getadminbyid(@PathVariable Integer uid,HttpSession session,Model model) {
		session.setAttribute("uid", uid);
		Admin_user users = adminservice.getuserByid(uid);
		model.addAttribute("admins", users);
		return "guanliyuanliebiaoxiugai";
	}
	//修改
	@RequestMapping("/modyadminbyid.html")
	public String modyadminbyid(@RequestParam(value="admin.qx")Integer ty_id,
			@RequestParam(value="admin.user")String au_name,
			@RequestParam(value="admin.password")String Password,
			HttpSession session,Model model) {
		int No = Integer.parseInt(session.getAttribute("uid").toString());
		Admin_user adn = (Admin_user)session.getAttribute("user");
		if (adn.getRole()==1) {
			Admin_user admin = adminservice.getuserByid(No);
			int role = (int)ty_id;
			admin.setRole(role);
			admin.setRoleName(au_name);
			admin.setRolePassword(Password);
			admin.setModifyday(new Date());
			System.out.println(ty_id+""+au_name+""+Password);
			if(adminservice.modifyadmin(admin)){
				return "redirect:/login/allofadmin.html";
			}
		} 
		model.addAttribute("error", "你没有相关的权限");
		return "redirect:/login/getadminbyid.html/"+No;
	}
	
	
	
	
	/**
	 * 删除管理�?
	 */
		@RequestMapping("/deladminofid.html")
		public String deladmin(@RequestParam("ids") Integer id, HttpSession session,Model model) {
			int No = id;
			Admin_user adn = (Admin_user)session.getAttribute("user");
			if (adn.getRole()==1) {
				if (adminservice.deladmins(id)) {
					return "redirect:/login/allofadmin.html";
				}
			}
			
			return "redirect:/login/allofadmin.html";
		}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
